import configparser
import math
import time
from sqlalchemy import Column, String, create_engine, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import requests

headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36",
    "cookie": ""
}

# 创建对象的基类:
Base = declarative_base()


class video_list(Base):
    # 表的名字:
    __tablename__ = 'video_list'

    # 表的结构:
    bvid = Column(String(20), primary_key=True)
    typeid = Column(Integer)
    author = Column(String(20))
    mid = Column(Integer)
    created = Column(Integer)
    length = Column(String(20))
    title = Column(String(100))
    is_union_video = Column(Integer)


class video_d(Base):
    # 表的名字:
    __tablename__ = 'video_d'

    # 表的结构:
    bvid = Column(String(20), primary_key=True)
    down_time = Column(Integer)
    state = Column(Integer)
    reply = Column(Integer)
    view = Column(Integer)
    danmaku = Column(Integer)
    favorite = Column(Integer)
    coin = Column(Integer)
    share = Column(Integer)
    like = Column(Integer)
    # now_rank = Column(Integer)
    # his_rank = Column(Integer)
    # evaluation = Column(Integer)


def bvid_list():
    bv_list = []
    engine = create_engine("mysql+pymysql://root:Aa380562113.@192.168.2.8:3306/bilibili?charset=utf8")
    DBSession = sessionmaker(bind=engine)
    session = DBSession()
    # 创建Query查询，filter是where条件，最后调用one()返回唯一行，如果调用all()则返回所有行:
    bv = session.query(video_list).filter().all()
    for i in bv:
        bv_list.append(i.bvid)
    session.close()
    return bv_list


def video_d_to_sql(cookie, bvid):
    url = "http://api.bilibili.com/x/web-interface/view?bvid={}"
    headers['cookie'] = cookie
    res = requests.get(url.format(bvid), headers=headers)
    res = res.json()
    if res.get("code") == 0:
        engine = create_engine("mysql+pymysql://root:Aa380562113.@192.168.2.8:3306/bilibili?charset=utf8")
        DBSession = sessionmaker(bind=engine)
        session = DBSession()
        new_video_d = video_d(bvid=bvid,
                              down_time=time.time(),
                              state=res.get("data").get("state"),
                              reply=res.get("data").get("stat").get("reply"),
                              view=res.get("data").get("stat").get("view"),
                              danmaku=res.get("data").get("stat").get("danmaku"),
                              favorite=res.get("data").get("stat").get("favorite"),
                              coin=res.get("data").get("stat").get("coin"),
                              share=res.get("data").get("stat").get("share"),
                              like=res.get("data").get("stat").get("like"),
                              # now_rank=res.get("data").get("stat").get("now_rank"),
                              # his_rank=res.get("data").get("stat").get("his_rank"),
                              # evaluation=res.get("data").get("stat").get("evaluation")
                              )
        session.add(new_video_d)
        session.commit()
        session.close()


def config():
    ck = []
    cp = configparser.RawConfigParser()
    cp.read('/root/bilibili/config.ini')
    # ck.append(cp.get('bilibili', 'cookie1'))
    # ck.append(cp.get('bilibili', 'cookie2'))
    ck.append(cp.get('bilibili', 'cookie3'))
    # up_list = cp.get('up_list', 'up_list')
    return ck


if __name__ == '__main__':
    cookie = config()[0]
    bv_list = bvid_list()
    for i in bv_list:
        video_d_to_sql(cookie, i)
        time.sleep(5)
